package io.renren.modules.store.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.modules.store.entity.TStoreFoodEntity;
import io.renren.modules.store.entity.TStoreFoodTypeEntity;
import io.renren.modules.store.service.TStoreFoodService;
import io.renren.modules.store.service.TStoreFoodTypeService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.store.entity.TStoreDiscountDetailEntity;
import io.renren.modules.store.service.TStoreDiscountDetailService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 21:22:21
 */
@RestController
@RequestMapping("store/tstorediscountdetail")
public class TStoreDiscountDetailController extends AbstractController {
    @Autowired
    private TStoreDiscountDetailService tStoreDiscountDetailService;
    @Autowired
    private TStoreFoodService tStoreFoodService;
    @Autowired
    private TStoreFoodTypeService tStoreFoodTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorediscountdetail:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreDiscountDetailService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 已经添加的菜品
     * @param id
     * @return
     */
    @RequestMapping("/alreadyAddFoods/{id}")
    @RequiresPermissions("store:tstorediscountdetail:list")
    public R alreadyAddFoods(@PathVariable("id") Long id){
        List<TStoreDiscountDetailEntity> list = tStoreDiscountDetailService.list(new QueryWrapper<TStoreDiscountDetailEntity>()
                .eq("seller_id", getSeller())
                .eq("discount_id", id)
        );
        List<Long> list1 = new ArrayList<>();
        for (TStoreDiscountDetailEntity tStoreDiscountDetailEntity : list) {
            list1.add(tStoreDiscountDetailEntity.getFoodId());
        }
        List<Map<String,Object>> totalList = new ArrayList<>();
        List<TStoreFoodTypeEntity> foodTypeList = tStoreFoodTypeService.list();
        for (TStoreFoodTypeEntity storeFoodTypeEntity : foodTypeList){
            Map<String,Object> map = new HashMap<String,Object>();
            List<TStoreFoodEntity> foodList = tStoreFoodService.list(new QueryWrapper<TStoreFoodEntity>()
                    .eq("seller_id", getSeller())
                    .eq("status", 1)
                    .eq("type_id", storeFoodTypeEntity.getId())
            );
            List<TStoreFoodEntity> foodEntityList = new ArrayList<>();
            for (TStoreFoodEntity storeFoodEntity : foodList){
                if (!list1.contains(storeFoodEntity.getId())){
                    foodEntityList.add(storeFoodEntity);
                }
            }
            if (foodEntityList.size() > 0){
                map.put("foodTypeName",storeFoodTypeEntity.getClassificationName());
                map.put("foodList",foodEntityList);
                totalList.add(map);
            }


        }
        return R.ok().put("foodList",totalList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstorediscountdetail:info")
    public R info(@PathVariable("id") Long id){
		TStoreDiscountDetailEntity tStoreDiscountDetail = tStoreDiscountDetailService.getById(id);
        List<Long> list = new ArrayList<Long>();
        list.add(tStoreDiscountDetail.getFoodId());
        tStoreDiscountDetail.setFoodList(list);
        return R.ok().put("tStoreDiscountDetail", tStoreDiscountDetail);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorediscountdetail:save")
    public R save(@RequestBody TStoreDiscountDetailEntity tStoreDiscountDetail){
        List<Long> foodList = tStoreDiscountDetail.getFoodList();
        List<TStoreDiscountDetailEntity> list = new ArrayList<>();
        for (Long foo : foodList){
            TStoreDiscountDetailEntity entity = new TStoreDiscountDetailEntity();
            //唯一标识
            String uniqueKey = tStoreDiscountDetail.getDiscountId().toString()+tStoreDiscountDetail.getDiscount().toString()
                    +foo.toString()+getSeller().toString();
            entity.setSellerId(getSeller());
            entity.setUniqueKey(uniqueKey);
            entity.setDiscountId(tStoreDiscountDetail.getDiscountId());
            entity.setDiscount(tStoreDiscountDetail.getDiscount());
            entity.setFoodId(foo);
            list.add(entity);
        }
        tStoreDiscountDetailService.saveOrUpdateBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorediscountdetail:update")
    public R update(@RequestBody TStoreDiscountDetailEntity tStoreDiscountDetail){
        tStoreDiscountDetailService.updateById(tStoreDiscountDetail);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorediscountdetail:delete")
    public R delete(@RequestBody Long[] ids){
		tStoreDiscountDetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
